#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeZeroSumSublists(ListNode* head) {
        ListNode* phead = new ListNode(0);
        phead->next = head;
        ListNode* cur = phead;
        while (cur != nullptr)
        {
            ListNode* tmp = cur->next;
            int sum = 0;
            while (tmp != nullptr)
            {
                sum += tmp->val;
                if (sum == 0)
                {
                    cur->next = tmp->next;
                }
                tmp = tmp->next;
            }
            cur = cur->next;
        }
        return phead->next;
    }
};